package com.hjyang.word.convert.pdf.controller;

import com.alibaba.fastjson.JSONObject;
import com.hjyang.word.convert.pdf.common.AccidentFile;
import com.hjyang.word.convert.pdf.common.Result;
import com.hjyang.word.convert.pdf.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/conver")
public class Word2PdfController {
    /**
     * 文件上传 绝对URL 根URL
     */
    @Value("${frtune.file.root.url}")
    private String fileRootUrl;
    /**
     * pdf存储位置
     */
    @Value("${frtune.word2pdf.file.path}")
    private String word2pdfFilePath;
    /**
     * pdf访问url
     */
    @Value("${frtune.word2pdf.file.url}")
    private String word2pdfFileUrl;
    /**
     * doc2pdf host
     */
    @Value("${frtune.word2pdf.doc.host}")
    private String word2pdfDocHost;
    /**
     * docx2pdf host
     */
    @Value("${frtune.word2pdf.docx.host}")
    private String word2pdfDocxHost;
    
    @Resource
    private RestTemplate restTemplate;
    
    /**
     * Word转Pdf
     *
     * @param fileId
     * @return
     */
   @RequestMapping(value = "/word2pdf", method = RequestMethod.GET)
    public Result word2pdf(String fileId) {
        if ("".equals(StringUtil.null2String(fileId))) {
            return new Result(400, "参数 fileId 为空");
        }
       //AccidentFile accidentFile = accidentFileService.getAccidentFileByFileId(fileId);
       AccidentFile accidentFile = new AccidentFile();
        if (null == accidentFile || "".equals(StringUtil.null2String(accidentFile.getFileSuffix()))) {
            return new Result(400, "参数 fileId 不正确");
        }
        if (!"doc".equals(accidentFile.getFileSuffix()) && !"docx".equals(accidentFile.getFileSuffix())) {
            return new Result(400, "参数 fileId 不正确");
        }
        String wordFilePath = accidentFile.getFileServerPath() + accidentFile.getFileServerName();
        
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String pdfName = fileId + "_" + new Date().getTime() + ".pdf";
        String pdfFilePath = word2pdfFilePath + today + "/" + pdfName;
        
        File pdfFile = new File(pdfFilePath);
        if (!pdfFile.getParentFile().exists()) {
            pdfFile.getParentFile().mkdirs();
        }
        
        JSONObject restJson = new JSONObject();
        String restUrl = "";
        if ("doc".equals(accidentFile.getFileSuffix())) {
            restUrl = word2pdfDocHost + "/conver/word2pdf/doc";
        } else if ("docx".equals(accidentFile.getFileSuffix())) {
            restUrl = word2pdfDocxHost + "/conver/word2pdf/docx";
        }
        MultiValueMap<String, String> restParam = new LinkedMultiValueMap<String, String>();
            restParam.add("wordFilePath", wordFilePath);
            restParam.add("pdfFilePath", pdfFilePath);
        HttpHeaders restHeaders = new HttpHeaders();// 设置 Header
            restHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(restParam, restHeaders);
        ResponseEntity<JSONObject> response = restTemplate.postForEntity(restUrl, requestEntity, JSONObject.class);
        
        if (null != response && null != response.getBody()){
            restJson = response.getBody();
            if (null != restJson.get("code") && "200".equals(restJson.get("code").toString())){
                JSONObject rstData = new JSONObject();
                rstData.put("pdfUrl", fileRootUrl + word2pdfFileUrl + today + "/" + pdfName);
                return new Result(200, "success", rstData);
            }
        }
        return new Result(400, "Word转Pdf失败！");
    }
}
